package com.example.test.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

import com.example.test.BindItemActivity;
import com.example.test.CarNumberPayListActivity;
import com.example.test.R;
import com.example.test.fragment.CarNumberFragment.ViewHolder;
import com.example.test.fragment.IncreaseFragment.CardListAdapter;
import com.example.test.utils.DbDaoUtils;
import com.lidroid.xutils.DbUtils.DaoConfig;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class ChepaiPayFragment extends BaseFragment implements OnClickListener{

	@ViewInject(R.id.btn_cheipaipay)
	private Button btn_chepaipay;
	@ViewInject(R.id.spinner_tingchechang)
	private Spinner spinner_tingchechang;
	@ViewInject(R.id.ly_carnumber)
	private LinearLayout ly_carnumber;
	@ViewInject(R.id.tv_isnotbind_carnumber)
	private TextView tv_isnotbind_carnumber;
	@ViewInject(R.id.tv_carnumber)
	private TextView tv_carnumber;
	
	private List<String> carnumberlist=new ArrayList<String>();
	private List<String> list = new ArrayList<String>();
	private ArrayAdapter<String> adapter;
	private PopupWindow popwin;
	private ListView listview;
	private Intent intent;
	@Override
	public void initData() {
		// TODO Auto-generated method stub
		DbDaoUtils dao=new DbDaoUtils(ct);
		carnumberlist=dao.findAllCarNumber();
		if(carnumberlist.isEmpty()){
			ly_carnumber.setVisibility(View.GONE);
			tv_isnotbind_carnumber.setVisibility(View.VISIBLE);
			
		}else{
		    tv_carnumber.setText(carnumberlist.get(0));
		    tv_carnumber.setBackgroundResource(ids2[0]);
		}
		list.add("test");
		list.add("test");
		list.add("test");
		list.add("XXtest");
		list.add("XXtest");
		list.add("XXtest");
		list.add("XXtest");
		list.add("XXtest");
	
		ly_carnumber.setOnClickListener(this);
		btn_chepaipay.setOnClickListener(this);
		tv_isnotbind_carnumber.setOnClickListener(this);
		inintlistview();
		adapter = new ArrayAdapter<String>(ct,
				android.R.layout.simple_spinner_item, list);
		
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner_tingchechang.setAdapter(adapter);
		spinner_tingchechang.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				view.setVisibility(View.VISIBLE); 
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				parent.setVisibility(View.VISIBLE); 
			}
		});
	}
	
	private void inintlistview() {
		// TODO Auto-generated method stub
		listview = new ListView(ct);
		CarNumberListAdapter myadapter = new CarNumberListAdapter();
		listview.setAdapter(myadapter);
	   
		listview.setVerticalScrollBarEnabled(false);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				tv_carnumber.setText(carnumberlist.get(position));
				tv_carnumber.setBackgroundResource(ids2[position]);
				popwin.dismiss();
			}
		});
	}

	@Override
	public View initView(LayoutInflater inflater) {
		
		// TODO Auto-generated method stub
		View view=inflater.inflate(R.layout.chepaipay_layout, null);
		ViewUtils.inject(this,view);
		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.ly_carnumber:
			popwin = new PopupWindow(ct);
			popwin.setWidth(ly_carnumber.getWidth());
			popwin.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
			
			popwin.setFocusable(true);
			popwin.setContentView(listview);
			popwin.setOutsideTouchable(true);
			popwin.showAsDropDown(ly_carnumber, 0, 0);
			break;
		case R.id.btn_cheipaipay:
			if(checkForm()){
				Intent intent=new Intent(ct, CarNumberPayListActivity.class);
				intent.putExtra("tingchechang", spinner_tingchechang.getSelectedItem().toString());
				intent.putExtra("carnumber", tv_carnumber.getText());
				startActivity(intent);
			}
			break;
		case R.id.tv_isnotbind_carnumber:
			intent = new Intent(ct, BindItemActivity.class);
			intent.putExtra("title", getString(R.string.bindcarnumber));
			startActivity(intent);
			break;
		default:
			break;
		}
	}
	
	private boolean checkForm() {
		// TODO Auto-generated method stub	    
		if (carnumberlist.isEmpty()) {
			Toast.makeText(ct, "请选择车牌", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	private int[] ids2 = { R.color.gridcolor1, R.color.gridcolor2,
			R.color.gridcolor3, R.color.gridcolor4, R.color.gridcolor5,
			R.color.gridcolor6 };
	class CarNumberListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return carnumberlist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView != null) {
				holder = (ViewHolder) convertView.getTag();
			} else {
				convertView = View.inflate(ct, R.layout.carnumberitem_layout,
						null);
				holder = new ViewHolder();
			
				holder.tv_carnumber = (TextView) convertView
						.findViewById(R.id.tv_carnumber);
				convertView.setTag(holder);
			}

			holder.tv_carnumber.setText(carnumberlist.get(position));
			int i=position%ids2.length;
		    if(i>=6){
		    	i=6;
		    }
			holder.tv_carnumber.setBackgroundResource(ids2[i]);
			return convertView;
		}

	}

	public static class ViewHolder {
		TextView tv_carnumber;
       
	}
}
